package org.jsola.hr.common;

import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import org.jsola.common.HashKit;
import org.jsola.exception.ParamException;
import org.json.JSONObject;

/**
 * @author wu
 */
public class JiNanSocialSecurity {


    /**
     * 济南社保 登录
     *
     * @param userId 用户id
     * @param pwd    密码
     * @return 返回信息
     */
    public static String getSocialSecurity(String userId, String pwd) {
        // 登录
        HttpResponse<String> socResponse = Unirest.post("http://wsfw.jnhrss.jinan.gov.cn:8001/hso/logon.do")
                .header("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
                .cookie("loginName", "0010093146")
                .body("method=doLogon&_xmlString=<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                        "<p><s userid=\"" + userId + "\"/>" +
                        "<s passwd=\"" + HashKit.md5(pwd) + "\"/></p>").asString();
        JSONObject rus = new JSONObject(socResponse.getBody());
        if (!rus.has("__usersession_uuid")) {
            throw new ParamException("调用社保登录接口失败");
        }
        return socResponse.getBody();
    }

    /**
     * 济南社保 获取dwdjid及jdjgid
     *
     * @param usersessionUuid usersessionUuid
     * @return 相关信息
     */
    public static String getDwdjidAndJdjgid(String usersessionUuid, String cookie) {
        // 获取dwdjid及jdjgid
        HttpResponse<String> socResponse = Unirest.post("http://wsfw.jnhrss.jinan.gov.cn:8001/hso/index.jsp")
                .header("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
                .header("Cookie", cookie)
                .body("_width=1920&_height=376&_random=0.2901582835490413&__usersession_uuid=" + usersessionUuid).asString();
        return socResponse.getBody();
    }

    /**
     * 济南社保 获取gridSessionID
     *
     * @param dwdjid          dwdjid
     * @param jdjgid          jdjgid
     * @param usersessionUuid usersessionUuid
     * @return 包含了gridSessionID的返回结果
     */
    public static String getGridSessionId(String method, String dwdjid, String jdjgid, String usersessionUuid, String cookie) {
        // 获取gridSessionID
        HttpResponse<String> socResponse = Unirest.post("http://wsfw.jnhrss.jinan.gov.cn:8001/hso/cxtjSi.do")
                .header("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
                .header("Cookie", cookie)
                .body("method=" + method +
                        "&dwdjid=" + dwdjid + "&jbjgid=" + jdjgid + "" +
                        "&_width=1710&_height=819" +
                        "&__usersession_uuid=" + usersessionUuid).asString();
        return socResponse.getBody();
    }

    /**
     * 济南社保 设置查询时间
     *
     * @param dwdjid          dwdjid
     * @param jdjgid          jdjgid
     * @param usersessionUuid usersessionUuid
     * @param gridSessionId   gridSessionId
     * @param startTime       开始时间 形如202001010000
     * @param endTime         结束时间 形如202003010000
     */
    public static void setParameter(String dwdjid, String jdjgid, String usersessionUuid, String gridSessionId,
                                    String startTime, String endTime, String cookie) {
        // 设置参数
        HttpResponse<String> socResponse = Unirest.post("http://wsfw.jnhrss.jinan.gov.cn:8001/hso/cxtjSi.do")
                .header("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
                .header("Cookie", cookie)
                .body("method=loadPayBills&_xmlString=<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                        "<p><s jbjgid=\"" + jdjgid + "\"/>" +
                        "<s qsrq=\"" + startTime + "\" zzrq=\"" + endTime + "\" djzt=\"1\" />" +
                        "<s dwdjid=\"" + dwdjid + "\"/></p>" +
                        "&__usersession_uuid=" + usersessionUuid).asString();


        HttpResponse<String> socResponse6 = Unirest.post("http://wsfw.jnhrss.jinan.gov.cn:8001/hso/sgrid.do")
                .header("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
                .header("Cookie", cookie)
                .body("method=fillSGridData&_xmlString=<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                        "<p><s tagsessionid=\"" + socResponse.getBody() + "\"/>" +
                        "<s tagdsname=\"jfxxds\"/>" +
                        "<s columnname=\"WkhkaWFBPT0sZW1Sc2MyZz0sZEhoeSxkSGh6YWc9PSxjWEp6YWc9PSxaR3A2ZEE9PSxjMkpxYW1wbCxZM3BxYW1wbCxlV05xYW1wbCxlbXBsLFlubz0%26%2361;\"/>" +
                        "<s gridSessionID=\"" + gridSessionId + "\"/>" +
                        "<s sumColumnsInfo=\"[]\"/>" +
                        "<s selectionMode=\"single\"/>" +
                        "<s gridSessionID=\"" + gridSessionId + "\"/></p>" +
                        "&_random=0.4038922180154503&__usersession_uuid=" + usersessionUuid).asString();
    }

    /**
     * 济南社保 根据已传的时间参数查询列表
     *
     * @param gridSessionId   gridSessionId
     * @param usersessionUuid usersessionUuid
     * @return 缴费月份列表
     */
    public static String getList(String gridSessionId, String usersessionUuid, String cookie) {
        // 查询列表信息
        HttpResponse<String> socResponse = Unirest.post("http://wsfw.jnhrss.jinan.gov.cn:8001/hso/sgrid.do?method=virtualScrollView&__usersession_uuid=" + usersessionUuid)
                .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .header("Cookie", cookie)
                .body("gridSessionID=" + gridSessionId + "&page=1&pageSize=25&updateBeginRowIndex=0&updateRows=%5B%5D").asString();
        return socResponse.getBody();
    }

    /**
     * 济南社保 设置查询缴费明细的参数
     *
     * @param jdjgid          jdjgid
     * @param serialNumberId  serialNumberId
     * @param usersessionUuid usersessionUuid
     * @return 携带了参数的字符串
     */
    public static String selectParameterBySerialNumberId(String jdjgid, String serialNumberId, String usersessionUuid, String cookie) {
        // 1: 设置参数
        HttpResponse<String> socResponse = Unirest.post("http://wsfw.jnhrss.jinan.gov.cn:8001/hso/cxtjSi.do")
                .header("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
                .header("Cookie", cookie)
                .body("method=queryPayBillDetlByZdlsh&_xmlString=<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                        "<p><s zdlsh=\"" + serialNumberId + "\"/>" +
                        "<s jbjgid=\"" + jdjgid + "\"/></p>" +
                        "&_width=1024&_height=316" +
                        "&_random=0.5394508629991037" +
                        "&__usersession_uuid=" + usersessionUuid).asString();
        return socResponse.getBody();
    }

    /**
     * 济南社保 获取查询缴费明细的gridSessionID
     *
     * @param param           自动流水号
     * @param jdjgid          jdjgid
     * @param usersessionUuid usersessionUuid
     * @return 包含了三个gridSessionId
     */
    public static String getGridSessionId2(String param, String jdjgid, String usersessionUuid, String cookie) {
        // 获取gridSessionID2
        HttpResponse<String> socResponse = Unirest.post("http://wsfw.jnhrss.jinan.gov.cn:8001/hso/cxtjSi.do")
                .header("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
                .header("Cookie", cookie)
                .body("method=fwdPayBillDetl&_xmlString=<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                        "<p><s zdlsh=\"" + param + "\"/>" +
                        "<s jbjgid=\"" + jdjgid + "\"/></p>" +
                        "&_width=1024&_height=316" +
                        "&_random=0.5394508629991037" +
                        "&__usersession_uuid=" + usersessionUuid).asString();
        return socResponse.getBody();
    }

    /**
     * 济南社保 查询缴费明细列表
     *
     * @param tagsessionid    设置参数接口返回的字符串
     * @param gridSessionId   gridSessionId
     * @param usersessionUuid usersessionUuid
     * @return 缴费明细列表
     */
    public static String selectPaymentDetailsBySerialNumberId(String tagsessionid, String gridSessionId,
                                                              String usersessionUuid, String cookie, Integer page, Integer pageSize) {
        // 1: 设置参数
        HttpResponse<String> socResponse = Unirest.post("http://wsfw.jnhrss.jinan.gov.cn:8001/hso/sgrid.do")
                .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .header("Cookie", cookie)
                .body("method=fillSGridData&_xmlString=<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                        "<p><s tagsessionid=\"" + tagsessionid + "\"/>" +
                        "<s tagdsname=\"plands\"/>" +
                        "<s columnname=\"WjNKaWFBPT0sZUcwPSxjMkpzWWc9PSxlSHBpZWc9PSxjWE51ZVE9PSxlbnB1ZVE9PSxaSGRxWm1weixaM0pxWm1weixaSGRxWm1wbCxaM0pxWm1wbA%26%2361;%26%2361;\"/>" +
                        "<s gridSessionID=\"" + gridSessionId + "\"/>" +
                        "<s sumColumnsInfo=\"[]\"/>" +
                        "<s selectionMode=\"single\"/>" +
                        "<s gridSessionID=\"" + gridSessionId + "\"/></p>" +
                        "&_random=0.29252938962321506&__usersession_uuid=" + usersessionUuid).asString();

        // 2：查询明细列表
        HttpResponse<String> socResponse1 = Unirest.post("http://wsfw.jnhrss.jinan.gov.cn:8001/hso/sgrid.do?" +
                "method=virtualScrollView&__usersession_uuid=" + usersessionUuid + "&page=" + page + "&pageSize=" + pageSize)
                .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .header("Cookie", cookie)
                .body("gridSessionID=" + gridSessionId).asString();
        return socResponse1.getBody();
    }

}
